var start = 'Whitefield, Bangalore, Karnataka, India';    
var directionsService = new google.maps.DirectionsService();
var map;

/*function publishWallPost(slug) {
    var attachment = {'name':'My Picture','description':'This is a test of the Facebook Broadcast System.  If this was real, you would be reading something useful.','media':[{'type':'image','src':'http://www.bighistory.net/wp-content/uploads/2010/01/Apple-Fruit.jpg','href':'http://www.MySuperCoolWebsite.com/' + slug + '/'}]};
    FB.ui({
        method: 'stream.publish',
        message: 'I just found this Super Cool Website!',
        attachment: attachment,
        user_message_prompt: 'post this to your wall?',
        app_id: '157461614311935'
    });
    
    
}*/

function publishWallPost(imagesrc){
	var my_message = 'My message here';
	var url = 'http://fbrell.com/f8.jpg';
	var title = 'Title';
	var desc = 'Description';
	alert(imagesrc);

	 FB.ui(
			   {
			     method: 'stream.publish',
			     name: 'Facebook Dialogs',
			     picture: imagesrc,
			     caption: 'Reference Documentation',
			     description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
			     message: 'Facebook Dialogs are easy!',
			     app_id: '236178669829324',
			    // access_token: 'AAACEdEose0cBAI7EZCdkbY00ewOSZCZAwG9yGqi8anY7KBuQBIuHulHyxLifcwDbKtAdFpKWPowq2UPbEhjYIZBN9coISxGMBpEiwVy3dCcSrTirkrTK'
			   },
			   function(response) {
			     if (response && response.post_id) {
			       alert('Post was published.');
			     } else {
			       alert('Post was not published.');
			     }
			   }
			 );
	 }


/*
function publishWallPost(imagesrc) {
	alert("publish : " + imagesrc);
	// calling the API ...
    var obj = {
      method: 'feed',
      link: 'https://developers.facebook.com/docs/reference/dialogs/',
      picture: 'http://fbrell.com/f8.jpg',
      name: 'Facebook Dialogs',
      caption: 'Reference Documentation',
      description: 'Using Dialogs to interact with users.',
    app_id: '157461614311935'
    };
    var timeoutVal = setTimeout(fbShowDialogs, 2000);

    FB.ui(
    		{
    		method: 'stream.publish',
    		message: 'getting educated about Facebook Connect',
    		attachment: {
    		name: 'Connect',
    		caption: 'The Facebook Connect JavaScript SDK',
    		description: (
    		'A small JavaScript library that allows you to harness ' +
    		'the power of Facebook, bringing the user\'s identity, ' +
    		'social graph and distribution power to your site.'
    		),
    		href: 'http://github.com/facebook/connect-js'
    		},
    		action_links: [
    		{ text: 'Code', href: 'http://github.com/facebook/connect-js' }
    		],
    		user_message_prompt: 'Share your thoughts about Connect'
    		},
    		function(response) {
    		if (response && response.post_id) {
    		alert('Post was published.');
    		} else {
    		alert('Post was not published.');
    		}
    		}
    		);

}*/

function facebooklogin(){
	alert("Facebook Login");
	document.forms[0].action = 'authAction';
	document.forms[0].submit(); 
}

function initialize() {

	var rendererOptions = {
			draggable: true
	};
	directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
	var bangalore = new google.maps.LatLng(12.9697612, 77.7508401);
	var myOptions = {
			zoom:12,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			center: bangalore
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	directionsDisplay.setMap(map);
	directionsDisplay.setPanel(document.getElementById("directionsPanel"));
	directionsDisplay.setPanel(window.parent.document.getElementById("directionsPanel"));
}

function detectLocation() { 
	// Try W3C Geolocation (Preferred)
	if(navigator.geolocation) {
		browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition(function(position) {
			start = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
			map.setCenter(start);
		}, function() {
			handleNoGeolocation(browserSupportFlag);
		});
		document.getElementById("locationmsg").hidden = true ;	
	}
} 

function computeTotalDistance(result) {
	var total = 0;
	var myroute = result.routes[0];
	for (i = 0; i < myroute.legs.length; i++) {
		total += myroute.legs[i].distance.value;
	}
	total = total / 1000;
	window.parent.document.getElementById("total").innerHTML = "Total Distance : " + total + " km";
}


function goback()  {
	var frame = document.getElementById("dataloader");
	var gobackto = frame.contentWindow.document.getElementById("gobackto").value;
	frame.contentWindow.document.getElementById(gobackto).scrollIntoView() ;
}

function getPlaces(){
	if(document.initDataLoader.selectedPlaceTypes.value==""){
		alert("Select a Place Type");
	}
	else if(document.initDataLoader.visitDate.value==""){
		alert("Enter Visiting Date");
	}
	else{
		//if(document.initDataLoader.selectedPlaceTypes.value)
		document.initDataLoader.actionFlag.value = "fetchplaces";
		document.forms[0].submit();
	}
}


function calcRoute(dest) {
	var request = {
			origin:start, 
			destination:dest,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
			computeTotalDistance(directionsDisplay.directions);
		}
		document.getElementById('map_canvas').scrollIntoView() ;
		document.getElementById('gobackto').value = dest ; 

	});

}
